/***********************************************************
**
** 作者：lth
** 日期：2016-03-09
** 描述：定义网络通讯中用到的常量、数据结构
**
************************************************************/

#ifndef SOCKETCONSTANT
#define SOCKETCONSTANT
#include <QByteArray>
#include <QList>
#include <QIODevice>
#include <QDataStream>

//Socket连接参数
#define NET_TCP_PORT             1234    //TCP服务器监听“起始”端口号
#define NET_PORT_SPACE           10      //尝试监听、绑定、广播端口号间隔

#define NET_SOCKET_TIMEOUT       10*1000 //多长时间没有收到客户端信息说明已经断开
#define NET_SHAKE_HAND_ENABLED   false


//包数据结构 : [ 包头 + 长度(8字节) + 包类型(2字节) + 数据 + 包尾 ]
#define NET_PACKET_START                "6200"
#define NET_PACKET_END_SEND             "55AA"
#define NET_PACKET_END_READ             "AA55"
#define NET_PACKET_TYPE_BYTES           4    //包类型占几个字节
#define NET_PACKET_LTNGTH_BYTES         4    //包数据长度占几个字节

//包类型
#define NET_PACKET_TYPE_KUOZENG               "0038" //扩增功能
#define NET_PACKET_TYPE_YANGBEN               "0039" //样本功能
#define NET_PACKET_TYPE_SHIYAN                "003A" //实验功能
#define NET_PACKET_TYPE_WEIHU                 "003B" //维护功能
#define NET_PACKET_TYPE_IP                    "003C" //IP&PORT功能

//控制命令
#define NET_CMD_ZIJIAN                   "0001"//自检
#define NET_CMD_STOP                     "0002"//开始
#define NET_CMD_CONTINUE                 "0003"//继续
#define NET_CMD_CLOSE                    "0004"//关闭
#define NET_CMD_WENDU                    "0005"//温度指令
#define NET_CMD_YINGGUANG                "0006"//荧光指令
#define NET_CMD_GUZHANG                  "0007"//故障指令

//维护命令
#define NET_WEIHU_KONG                     "0000"//占位用
#define NET_WEIHU_JIARESI                  "0080"//加热丝
#define NET_WEIHU_SERSORWENDU              "0081"//传感器温度
#define NET_WEIHU_RAOLUANFENGSHAN          "0082"//扰乱风扇
#define NET_WEIHU_RAOLUANFENGSHANKAIGUAN   "0083"//扰乱风扇开关
#define NET_WEIHU_PAIFENGFENGSHAN          "0084"//排风风扇
#define NET_WEIHU_DUILIUFENGSHAN           "0085"//对流风扇
#define NET_WEIHU_BIAOZHUNWENDU            "0086"//标准温度传感器
#define NET_WEIHU_RESHIYUANDIAN            "0087"//热室原点位置
#define NET_WEIHU_XUANZHUANDIANJI          "0088"//孔位偏差校正

#define NET_WEIHU_TONGDAOKAIGUAN           "0089"//通道开关
#define NET_WEIHU_TONGDAOZHI               "008A"//通道值
#define NET_WEIHU_JIFAQIANGDU              "008B"//激发强度
#define NET_WEIHU_YINGGUANGZENGYI          "008C"//荧光增益

#define NET_WEIHU_MENSUO                   "008D"//门锁
#define NET_WEIHU_BIGAIJIANCE              "008E"//闭盖检测
#define NET_WEIHU_MENSUOJIAOZHENG          "008F"//门锁校正
//用于存储接收到的数据包
class SocketPacket
{
public:
    qint16     length;   //数据长度
    QByteArray data;     //数据
    QByteArray dataType; //数据类型

    SocketPacket()
    {
        length = -1;
    }

    void clear()
    {
        length = -1;
        data.clear();
        dataType.clear();
    }
};

struct  KongweiData
{
    QString  position;
    KongweiData()
    {
        position.clear();
    }
};




struct  YingguangData
{
    QString stage;
    QString step;
    QString xunhuanshu;
    //struct KongweiData *kongweimsg[8];
    QVector<QVector<QString>> kongweimsg;

    YingguangData()
    {
        stage.clear();
        step.clear();
        xunhuanshu.clear();
        kongweimsg.clear();

    }
};

Q_DECLARE_METATYPE(YingguangData);
struct NetLedControl
{
    bool led1;
    bool led2;
    bool led3;
};
struct messageData
{
    QString deviceID;
    QString shiyanID;
    QString status;

    messageData()
    {
        deviceID.clear();
        shiyanID.clear();
        status.clear();
    }
};

class NetSystemStatus
{
public:
    float temperature;
    float pressure;

    bool led1;
    bool led2;
    bool led3;

    //写数据到类数据成员中
    void write(QByteArray *data)
    {
        QDataStream streamWriter(data, QIODevice::WriteOnly);
        streamWriter.setVersion(QDataStream::Qt_4_3);

        streamWriter << temperature;
        streamWriter << pressure;
        streamWriter << led1;
        streamWriter << led2;
        streamWriter << led3;
    }

    //读数据到类数据成员中
    void read(QByteArray &data)
    {
        QDataStream streamReader(&data, QIODevice::ReadOnly);
        streamReader.setVersion(QDataStream::Qt_4_3);

        streamReader >> temperature;
        streamReader >> pressure;
        streamReader >> led1;
        streamReader >> led2;
        streamReader >> led3;
    }

};

class NetText
{
public:
    QString m_text;

    //写数据到类数据成员中
    void write(QByteArray *data)
    {
        QDataStream streamWriter(data, QIODevice::WriteOnly);
        //streamWriter.setVersion(QDataStream::Qt_4_3);

        streamWriter << m_text;
    }

    //读数据到类数据成员中
    void read(QByteArray &data)
    {
        QDataStream streamReader(&data, QIODevice::ReadOnly);
        //streamReader.setVersion(QDataStream::Qt_4_3);

        streamReader >> m_text;
    }
};


#endif // SOCKETCONSTANT

